import { _decorator, Component, Node } from 'cc';
import { EnemyState } from './EnemyState';
import { Enemy } from './Enemy';
import { EnemyStateMachine } from './EnemyStateMachine';
const { ccclass, property } = _decorator;

@ccclass('EnemyBattleState')
export class EnemyBattleState extends EnemyState {

    constructor(_enemy: Enemy, _stateMachine: EnemyStateMachine) {
        super(_enemy, _stateMachine);
    }

    override enter(): void {
        super.enter();
        this.enemy.playAnimation("yidong", true);
    }

    override update(dt: number): void {
        super.update(dt);
        this.enemy.SetVelocity(0, 0); // 停止移动

        if (this.enemy.isPlayerInAttackRange() && this.canAttack()) {
            this.stateMachine.changeState(this.enemy.attackState);

        } else if (!this.enemy.isPlayerInAttackRange()) {
            this.stateMachine.changeState(this.enemy.moveState);
        }


    }

    override exit(): void {
        super.exit();
    }

    canAttack(): boolean {
        const currentTime = Date.now() / 1000; // 当前时间（秒）
        if (currentTime >= this.enemy.lastAttackTime + this.enemy.attackCooldown) {
            this.enemy.lastAttackTime = currentTime;
            return true;
        }
        return false;
    }
}


